package com.bong.panpan.bongcharttools.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.bong.panpan.bongcharttools.ble.BleLink;
import com.bong.panpan.bongcharttools.R;
import com.bong.panpan.bongcharttools.custom.SpUtil;
import com.bong.panpan.bongcharttools.util.Common;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * A fragment with a Google +1 button.
 * Activities that contain this fragment must implement the
 * {@link OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link BleServerSettingFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BleServerSettingFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    @InjectView(R.id.rx_server_edit)
    EditText rxServerEdit;
    @InjectView(R.id.rx_char_edit)
    EditText rxCharEdit;
    @InjectView(R.id.tx_server_edit)
    EditText txServerEdit;
    @InjectView(R.id.tx_char_edit)
    EditText txCharEdit;
    @InjectView(R.id.ble_set_ok_btn)
    Button bleSetOkBtn;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public BleServerSettingFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BleServerSettingFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BleServerSettingFragment newInstance(String param1, String param2) {
        BleServerSettingFragment fragment = new BleServerSettingFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_ble_server_setting, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshEdittext();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    public void refreshEdittext() {
        String rxServerUUID = SpUtil.getInstance().getStrng("RxServerUUID", "6e400001-b5a3-f393-e0a9-e50e24dcca1e");
        String rxCharUUID = SpUtil.getInstance().getStrng("RxCharUUID", "6e400002-b5a3-f393-e0a9-e50e24dcca1e");
        String txServerUUID = SpUtil.getInstance().getStrng("TxServerUUID", "6e400001-b5a3-f393-e0a9-e50e24dcca1e");
        String txCharUUID = SpUtil.getInstance().getStrng("TxCharUUID", "6e400003-b5a3-f393-e0a9-e50e24dcca1e");
        rxServerEdit.setText(rxServerUUID);
        txServerEdit.setText(txServerUUID);
        rxCharEdit.setText(rxCharUUID);
        txCharEdit.setText(txCharUUID);
        BleLink.RX_SERVER_UUID = rxServerUUID;
        BleLink.TX_SERVER_UUID = txServerUUID;
        BleLink.RX_UUID = rxCharUUID;
        BleLink.TX_UUID = txCharUUID;
    }

    private boolean isUUIDRight(String uuid) {
        if (uuid.length() != 36)
            return false;
        String temp[] = uuid.split("-");
        if (temp == null || temp.length != 5)
            return false;
        if (temp[0].length() != 8 ||
                temp[1].length() != 4 ||
                temp[2].length() != 4 ||
                temp[3].length() != 4 ||
                temp[4].length() != 12)
            return false;
        return true;
    }

    @OnClick(R.id.ble_set_ok_btn)
    public void onViewClicked() {
        String rxServerUUID = rxServerEdit.getText().toString();
        String rxCharUUID = rxCharEdit.getText().toString();
        String txServerUUID = txServerEdit.getText().toString();
        String txCharUUID = txCharEdit.getText().toString();
        if (!isUUIDRight(rxServerUUID)) {
            Common.showToast(getActivity(), "RxServerUUID格式错误");
            return;
        }
        if (!isUUIDRight(txServerUUID)) {
            Common.showToast(getActivity(), "TxServerUUID格式错误");
            return;
        }
        if (!isUUIDRight(rxCharUUID)) {
            Common.showToast(getActivity(), "RxCharUUID格式错误");
            return;
        }
        if (!isUUIDRight(txCharUUID)) {
            Common.showToast(getActivity(), "TxCharUUID格式错误");
            return;
        }
        if (mListener != null) {
            mListener.onBleSetFragmentInteraction();
        }
        SpUtil.getInstance().setStrng("RxServerUUID", rxServerUUID);
        SpUtil.getInstance().setStrng("TxServerUUID", txServerUUID);
        SpUtil.getInstance().setStrng("RxCharUUID", rxCharUUID);
        SpUtil.getInstance().setStrng("TxCharUUID", txCharUUID);
        BleLink.RX_SERVER_UUID = rxServerUUID;
        BleLink.TX_SERVER_UUID = txServerUUID;
        BleLink.RX_UUID = rxCharUUID;
        BleLink.TX_UUID = txCharUUID;
        Common.showToast(getActivity(), "设置成功");
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onBleSetFragmentInteraction();
    }

}
